//void, Obj This
Unit this;
int max_fish;

Sleep(5960+rand(GetConst("RandTimeStart"))+GetConst("AddTimeStart"));

this = This.AsUnit;

while(!.IsValid) Sleep(10000); 
//pr("fishing boat");
max_fish = 500;
while(.IsAlive) {
	
	Sleep(rand(200) + 1800);
        if(!.InHolder){
        	Query q;   
                
        	q = Intersect(ObjsInCircle(.pos, 1400, "BaseShipyard"), ClassPlayerObjs("BaseShipyard", .player));       
	        if(q.IsEmpty()){
	                if(.command == "idle"){
	                        
        			if(.AsWagon.amount < max_fish){
                                	
                                	while(.AsWagon.amount < max_fish){
                                        	
	                        		if(.command != "idle") break;
                                                .AsWagon.SetFood(.AsWagon.amount+4);
                                                Sleep(2000);
                                        }
                                        if(.AsWagon.amount > max_fish)
                                		.AsWagon.SetFood(max_fish);
	                                if(.AsWagon.amount >= max_fish){    // 1 minuta i 40 sek
	                                        Settlement sett_target;
	                                        str target_name;
	                                        
	                                        //pr(.AsWagon.name);
	                                        target_name = ""+.AsWagon.name;
	                                        if(target_name != ""){
	                                                sett_target = GetNamedObj(target_name).obj.AsBuilding.settlement;
	                                                if(sett_target.player == .player)
	                                                        .SetCommand("unload", sett_target.GetCentralBuilding());
                                                        else
                                                        	Sleep(30000);
	                                        }
                                                
	                                }
                                }
	                }  
	        } else {
                	if(.AsWagon.amount < max_fish){
                		point pt;
                                int r, s, s2;
                        
                        	s = 1430;
                        	s2 = -1430;
                        	r = rand(4);
                        
                        	if(r == 0) 
	                                pt = .AsWagon.posRH+Point(s, s);
	                        else if(r == 1)
                                	pt = .AsWagon.posRH+Point(s2, s2);
	                        else if(r == 2)
                                	pt = .AsWagon.posRH+Point(s, s2);
	                        else 
                                	pt = .AsWagon.posRH+Point(s2, s);
                                //if(IsPointInWater(pt)){
                                	.SetCommand("move", pt);
	                                while(.command == "move") { Sleep(1000); }
                                //}
	                        //Sleep(20000);
                        }
                  
                } 
        } else {
        	point ptGo;
                
                ptGo = .AsWagon.GetHolderSett.GetCentralBuilding.GetExitVector(); 
                ptGo.Set( ptGo.x * 5, ptGo.y * 5 );
                
                .AsWagon.AddCommand( false, "move", .AsWagon.GetHolderSett.GetCentralBuilding.pos + ptGo );
		.AsWagon.KillCommand(); 
        
        }

}
